package com.bluexmicro.example.route

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.bluexmicro.example.page.help.HelpPage
import com.bluexmicro.example.page.main.MainPage
import com.bluexmicro.example.page.main.MainViewModel
import com.bluexmicro.example.page.pick.file.FilePicker
import com.bluexmicro.example.page.pick.peripheral.PeripheralPage
import com.bluexmicro.example.page.transaction.TransactionPage
import com.bluexmicro.example.page.transaction.TransactionViewModel

@Composable
fun NavigationHost(
    navHostController: NavHostController,
) {
    val context = LocalContext.current
    val mainViewModel = viewModel<MainViewModel>()
    val transactionViewModel = viewModel<TransactionViewModel>()
    LaunchedEffect(Unit) {
        mainViewModel.loadLocalTasks(context)
        mainViewModel.loadLocalFastMode(context)
        transactionViewModel.loadLocalTasks(context)
    }
    NavHost(
        navHostController,
        startDestination = Route.OTA.key,
    ) {
        composable(Route.OTA.key) {
            MainPage(navHostController, mainViewModel)
        }
        composable(Route.OTA_PICK_PERIPHERAL.key) {
            PeripheralPage(navHostController) {
                mainViewModel.selectedDevice.value = it
                navHostController.popBackStack()
            }
        }
        composable(Route.OTA_PICK_FILE.key) {
            FilePicker(onClose = {
                navHostController.popBackStack()
            }, onSelected = { file, address ->
                mainViewModel.selectFile(context, file, address)
                navHostController.popBackStack()
            })
        }
        /////////////////////////////////////
        composable(Route.OTA_TRANSACTION.key) {
            TransactionPage(navHostController, transactionViewModel)
        }
        composable(Route.OTA_PICK_PERIPHERAL_TRANSACTION.key) {
            PeripheralPage(navHostController) {
                transactionViewModel.selectedDevice.value = it
                navHostController.popBackStack()
            }
        }
        composable(Route.OTA_PICK_FILE_TRANSACTION.key) {
            FilePicker(onClose = {
                navHostController.popBackStack()
            }, onSelected = { file, address ->
                transactionViewModel.selectFile(context, file, address)
                navHostController.popBackStack()
            })
        }
        composable(Route.HELP.key) {
            HelpPage()
        }

    }
}

enum class Route(
    val key: String
) {
    OTA("OTA_NORMAL"),
    OTA_PICK_PERIPHERAL("OTA_PICK_VERSION_NORMAL"),
    OTA_PICK_FILE("OTA_PICK_FILE_NORMAL"),

    OTA_TRANSACTION("OTA_TRANSACTION"),
    OTA_PICK_PERIPHERAL_TRANSACTION("OTA_PICK_VERSION_TRANSACTION"),
    OTA_PICK_FILE_TRANSACTION("OTA_PICK_FILE_TRANSACTION"),


    HELP("HELP")
}



